//
//  Camera.m
//  SpaceStorm
//
//  Created by 祥 雍 on 13-6-15.
//  Copyright (c) 2013年 __MyCompanyName__. All rights reserved.
//
//
//  镜头类，移动，缩放，每个层得响应

#import "Camera.h"
#import "cocos2d.h"
#import "Constants.h"
#import "BattleScene.h"
#import "Variable.h"
#import "FollowMoveToAction.h"

@implementation Camera

@synthesize width =_width;
@synthesize height = _height;
@synthesize x = _x;
@synthesize y = _y;

@synthesize curWidth =_curWidth;
@synthesize curHeight = _curHeight;
@synthesize curX = _curX;
@synthesize curY = _curY;

/**
    根据初始宽，高，x,y,返回camera得实例
	@param width 初始camera的宽
	@param height 初始camera的高
	@param x 初始camera的x
	@param y 初始camera的y
	@returns 返回 camera的实例
 */
+(id) cameraWithWidth:(float)width height:(float)height x:(float)x y:(float)y
{
    return [[[self alloc] initWithWidth:width height:height x:x y:y] autorelease]; 
}


/**
	根据初始宽，高，x,y,初始化camera的实例
	@param width 初始camera的宽
	@param height 初始camera的高
	@param x 初始camera的x
	@param y 初始camera的y
	@returns 返回 camera的实例
 */
-(id) initWithWidth:(float)width height:(float)height x:(float)x y:(float)y
{
    self = [super init]; 
	
	if (self!= nil) 
    {
        self.width = width;
        self.height = height;
        self.x = x;
        self.y = y;
        
        self.curWidth = width;
        self.curHeight = height;
        self.curX = x;
        self.curY = y;
        
        _centerX = width / 2;
        _centerY = height / 2;
        
        //初始化跟随节点
        _followNode = nil;
        _isFollow = NO;
        
        //启动每帧的跟随检查
        [self scheduleUpdate];
    }
    
    return self;
}


/**
	处理视图模式的改变
	@param changeToType 试图的模式
 */
-(void) updateViewType:(BattleViewType)changeToType
{
    float beforeCenterX = (self.curX / self.width + 0.5) * self.curWidth;
    float beforeCenterY = (self.curY / self.height + 0.5) * self.curHeight;
    
    switch (changeToType) 
    {
        case BattleViewMicroscopic://微观视图(1/1)
        {
            self.curWidth = self.width;
            self.curHeight = self.height;
            break; 
        }
        case BattleViewLocal://局部视图(1/0.44)
        {
            self.curWidth = self.width / localScale;
            self.curHeight = self.height / localScale;
            break; 
        }
        case BattleViewMacroscopic://宏观视图(1/0.22)
        {
            self.curWidth = self.width / macroscopicScale;
            self.curHeight = self.height / macroscopicScale;
            break; 
        }
        case BattleViewOverallSituation://全局视图(1/0.15)
        {
            self.curWidth = self.width / overallSituationScale;
            self.curHeight = self.height / overallSituationScale;
            break; 
        }
        default:
            break;
    }
    
    [self setViewpointCenter:ccp(beforeCenterX, beforeCenterY)];
    CCLOG(@"Camera：处理视图模式改变");
}

/**
	设置视图的中心点
	@param position 视图中心点
 */
-(void) setViewpointCenter:(CGPoint)position
{
    self.curX = (position.x /self.curWidth - 0.5)  * self.width;
    self.curY = (position.y /self.curHeight - 0.5) * self.height;
    
    //根据镜头中心，调整各层的坐标
    [[BattleScene sharedLayer] setViewpointCenter:position];
}

/**
	缓动设置视图中心点，并设置跟随对象
	@param position 视图的中心点
 */
-(void) setViewpointCenterEase:(CGPoint)position

{
    id actX = [CCActionTween actionWithDuration:1 key:@"centerX" from:self.centerX to:position.x];  
    id actY = [CCActionTween actionWithDuration:1 key:@"centerY" from:self.centerY to:position.y];  
//    CCEaseInOut *easeX = [CCEaseInOut actionWithAction:actX rate:5];
//    CCEaseInOut *easeY = [CCEaseInOut actionWithAction:actY rate:5];
    [self runAction:actX]; 
    [self runAction:actY]; 
}

-(void) onNearTarget
{
    if(_followNode != nil)
    {
        id actX = [CCActionTween actionWithDuration:0.5 key:@"centerX" from:self.centerX to:_followNode.position.x];  
        id actY = [CCActionTween actionWithDuration:0.5 key:@"centerY" from:self.centerY to:_followNode.position.y];  
        CCCallFunc *moveAt = [CCCallFunc actionWithTarget:self selector:@selector(onMoveAtTarget)];  
        CCSequence *sequence = [CCSequence actions:actY, moveAt, nil]; 
        [self runAction:actX]; 
        [self runAction:sequence]; 
    }
}

-(void) onMoveAtTarget
{
    //打开跟随开关
    _isFollow = YES;
}

/**
	设置镜头的跟随对象，会先缓动到对象的坐标那，然后再镜头跟随
	@param followNode 镜头跟随对象
 */
-(void) setFollowNode:(CCNode*)followNode
{
    //先取消follow的选定
    [self unFollowNode];
    
    //设定跟随对象并retain
    _followNode = followNode;
    [_followNode retain];
    
    //添加动作
    id act = [FollowMoveToAction actionWithDuration:2 followTarget:_followNode];
    CCEaseInOut *easeX = [CCEaseInOut actionWithAction:act rate:5];
    CCCallFunc *moveAt = [CCCallFunc actionWithTarget:self selector:@selector(onMoveAtTarget)];
    CCSequence *sequence = [CCSequence actions:easeX, moveAt, nil];
    [self runAction:sequence];
}

/**
 取消镜头跟随对象
 */
-(void) unFollowNode
{
    //先停止镜头所有动作
    [self stopAllActions];
    
    //关闭跟随开关
    _isFollow = NO;
    
    //再释放，并置空followNode的引用
    if(_followNode)//因为会对设置进来的followNode做retain处理，所以要release
    {
        [_followNode release];
        _followNode = nil;
    }
}

/**
 每帧更新，如果有跟随单位的话，跟随之
	@param delta 
 */
-(void) update:(ccTime)delta
{
    if(_followNode && _isFollow == YES)
    {
        [self setViewpointCenter:_followNode.position];
    }
}

/**
	设置镜头的中心点X
	@param centerX 逻辑中心点X坐标
 */
-(void) setCenterX:(float)centerX

{
    self.curX = (centerX /self.curWidth - 0.5)  * self.width;
   
    _centerX = centerX;
}

-(float) centerX
{
    return _centerX;
}

/**
	设置镜头的中心点Y
	@param centerY 逻辑中心点Y坐标
 */
-(void) setCenterY:(float)centerY

{
    self.curY = (centerY /self.curHeight - 0.5) * self.height;
    
    _centerY = centerY;
    
    //根据镜头中心，调整各层的坐标
    [[BattleScene sharedLayer] setViewpointCenter:ccp(_centerX, _centerY)];
}

-(float) centerY
{
    return _centerY;
}

-(void) dealloc
{
    if(_followNode)//因为会对设置进来的followNode做retain处理，所以要release
    {
        [_followNode release];
        _followNode = nil;
    }
    // 不要忘记调用"super dealloc" 
    [super dealloc];
}

@end
